package com.mc.cloud.mall.categoryProduct.controller;

import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.categoryProduct.model.pojo.Category;
import com.mc.cloud.mall.categoryProduct.model.vo.AddCategory;
import com.mc.cloud.mall.categoryProduct.model.vo.CategoryVO;
import com.mc.cloud.mall.categoryProduct.model.vo.UpdateCategory;
import com.mc.cloud.mall.categoryProduct.service.CategoryService;
import com.mc.cloud.mall.common.common.MallRestResponse;
import com.mc.cloud.mall.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CategoryController
 * @Author MC
 * @Date 2021/5/22 18:24
 * @Version 1.0
 **/
@Controller
public class CategoryController {
    
    
    @Autowired
    CategoryService categoryService;
    
    /**
     * 后台添加目录
     * @param addCategory
     * @return
     */
    @ApiOperation("后台添加商品分类")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public MallRestResponse addCategory(@Valid @RequestBody AddCategory addCategory){
        
        categoryService.addCategory(addCategory);
        return MallRestResponse.success();
        
    }
    
    
    
    /**
     * 后台更新目录
     * @param updateCategory
     * @return
     */
    @ApiOperation("后台更新目录")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public MallRestResponse updateCategory(@Valid @RequestBody UpdateCategory updateCategory){
        
        Category category = new Category();
        BeanUtils.copyProperties(updateCategory,category);
        categoryService.updateCategory(category);
        return MallRestResponse.success();
    }
    
    /**
     * 后台删除目录
     * @param id
     * @return
     */
    @ApiOperation("后台删除目录")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public MallRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.deleteCategory(id);
        return MallRestResponse.success();
    }
    
    /**
     * 分类列表
     * @param pageNum
     * @param size
     * @return
     */
    @ApiOperation("后台目录列表")
    @GetMapping("/admin/category/list")
    @ResponseBody
    public MallRestResponse listCategoryForAdmin(@RequestParam Integer pageNum, @RequestParam Integer size){
        
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, size);
        
        return MallRestResponse.success(pageInfo);
    }
    
    /**
     * 用户分类列表
     * @return
     */
    @ApiOperation("用户分类列表")
    @GetMapping("/category/list")
    @ResponseBody
    public MallRestResponse<List<CategoryVO>> listCategoryForCustom(){
        
        List<CategoryVO> categoryVOS = categoryService.listForCustom(0);
        return MallRestResponse.success(categoryVOS);
    }
}
